package com.example.controller;


import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.example.base.aspect.AutoLog;
import com.example.base.response.Result;
import com.example.entity.Params;
import com.example.entity.Pet;
import com.example.service.PetService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@CrossOrigin
@RestController
@RequestMapping("/pet")
@Api("宠物信息管理")
public class PetController {
    @Autowired
    private PetService petService;
    @GetMapping("/finder")
    public Result findAll() {
        return Result.success(petService.findAll());
    }

    @GetMapping("/search")
    public Result findBySearch(Params params) {
        PageInfo<Pet> info = petService.findBySearch(params);
        return Result.success(info);
    }


    @PostMapping
    public Result save(@RequestBody Pet pet) {
        if (pet.getId() == null) {
            petService.add(pet);
        } else {
            petService.update(pet);
        }
        return Result.success();
    }

    @PostMapping("/update")
    public Result update(@RequestBody Pet pet) {

        petService.update(pet);

        return Result.success();
    }

    @DeleteMapping("/{id}")
    @AutoLog("删除宠物信息")
    public Result delete(@PathVariable Integer id) {
        petService.delete(id);
        return Result.success();
    }


    @GetMapping("/echarts/circle")
    public Result circle() {

        //
        List<Pet> list = petService.findAll();
        Map<String, Long> collect = list.stream().filter(x -> ObjectUtil.isNotEmpty(x.getTypeName()))
                .collect(Collectors.groupingBy(Pet::getTypeName, Collectors.counting()));

        List<Map<String, Object>> mapList = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(collect)) {
            for (String key : collect.keySet()) {
                Map<String, Object> map = new HashMap<>();
                map.put("name", key);
                map.put("value", collect.get(key));
                mapList.add(map);
            }
        }
        return Result.success(mapList);
    }

    @GetMapping("/echarts/bar")
    public Result bar() {

        //
        List<Pet> list = petService.findAll();
        Map<String, Long> collect = list.stream().filter(x -> ObjectUtil.isNotEmpty(x.getTypeName()))
                .collect(Collectors.groupingBy(Pet::getTypeName, Collectors.counting()));


        List<String> xAxis = new ArrayList<>();
        List<Long> yAxis = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(collect)) {
            for (String key : collect.keySet()) {
                xAxis.add(key);
                yAxis.add(collect.get(key));

            }
        }
        Map<String, Object> map = new HashMap<>();
        map.put("xAxis", xAxis);
        map.put("yAxis", yAxis);
        return Result.success(map);
    }
}
